<?php
class User_Model_User
{

	private $_table;
	//Initalizing Db Table  
	public function User_Model_User(){
		$this->_table = new User_Model_DbTable_User();
	}
	
	
	/**
	*Create A Record  
	*@param rowSet $row Department's data 
	*@return boolean
	*/
	public  function add($row){	 
 		return $this->_table->insert($row); 
	}
	
	/**
	*Update One Row  
	*@param rowSet $row Department's data 
	*@param int $ud_id 
	*@return boolean
	*/
	public  function update($row,$user_id){
		$where = $this->_table->getAdapter()->quoteInto('user_id = ?', $user_id);
		return $this->_table->update($row, $where);	
	}
		
	/**
	*No Delete One Row
	*@param int $ud_id 
	*@return boolean
	*/
	 public function delete($id){	
		$where = $this->_table->getAdapter()->quoteInto('user_id = ?', $id); 
		return $this->_table->delete($where);		
	}
	
	public function getByUserId($userId){	
		$select = $this->_table->select()->where('user_id = ?', $userId);	 
		return $this->_table->fetchRow($select);		
	}
	
	/**
	*Get Rowset By Department Name
	*@param string $ud_id 部门id
	*@return Rowset
	*/
	public function getByName($departmentName){
	
		$select = $this->_table->select()->where('ud_name = ?', $departmentName);	 
		
		return $this->_table->fetchRow($select);
		
	}
	
	/**
	*Get Rowset By user 
	*@param string $ud_id login id
	*@return Rowset
	*/
	public function getByLoginId($id){
		$select = $this->_table->select()->where('login_id = ?', $id);	 
		return $this->_table->fetchRow($select);
	}
	
	/*
	*get List
	*/
	public function getList($cond,$type='*' ,$order='user_id', $offset =0, $pageSize=0){	

		$select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
		if($type!='*') $type= 'count(*)';
        $select->from($table,$type);
        
		if(isset($cond['ud_id']) && $cond['ud_id']!==''){
			$select->where('ud_id = ?', $cond['ud_id']);
		}
		if(isset($cond['status']) && $cond['status'] !==''){
			$select->where('status = ?', $cond['status']);
		}
		if(isset($cond['login_id']) && $cond['login_id'] !=''){
			$select->where('login_id = ?', $cond['login_id']);
		}
		
		if(isset($order)) $select->order($order);
		if($pageSize>0 and $offset>=0){			
			$select->limit($pageSize,$offset);
		}
		if($type=='*'){
			return $this->_table->getAdapter()->fetchAll($select->__toString());
		}else {
			return $this->_table->getAdapter()->fetchOne($select->__toString());
		}
		
	}
	
	public function getByCond($cond,$type,$order='user_id',$count=20, $offset=0){
		$table = $this->_table->info('name');
		$select = $this->_table->select()->from($table, $type);
		if(isset($cond['ud_id']) && $cond['ud_id']!==''){
			$select->where('ud_id = ?',$cond['ud_id']);
		}
		if(isset($cond['status']) && $cond['status'] !==''){
			$select->where('status = ?',$cond['status']);
		}
		if('count(*)'==$type){
			return $this->_table->getAdapter()->fetchOne($select);
		}
		if ($count==0){
			return $this->_table->fetchAll($select)->toArray();
		}
		$select->order($order);
		$select->limit($count,$offset);
		$rowset = $this->_table->fetchAll($select);
		return $rowset->toArray();
	}
	
	public function getByfield($field=1,$value=0){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value);
		return $this->_table->fetchAll($where);
	}
	
}